/***************************************************************************
  A SymbolBrowser plugin for JuffEd editor.

  Copyright: 2010 Aleksander Sokoloff <asokol@mail.ru>


  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License.
  version 2 as published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
***************************************************************************/

#include "katesymbolbrowserview.h"
#include "katesymbolbrowserplugin.h"
#include "common/symbolbrowserview.h"
#include "katedocumentparser.h"
#include <ktexteditor/document.h>
#include <ktexteditor/view.h>
#include <klocale.h>
#include <kconfiggroup.h>

#include <QStringList>
#include <QPixmap>
#include <QListWidget>
#include <QDebug>


/**********************************************************
 *
 **********************************************************/
KateSymbolBrowserView::KateSymbolBrowserView(KateSymbolBrowserPlugin *plugin, Kate::MainWindow *mainWindow)
: Kate::PluginView(mainWindow)
, KXMLGUIClient()
{
    mMainView = mainWindow->createToolView("kate_private_plugin_symbolbrowser_symbols",
                                            Kate::MainWindow::Left,
                                            QPixmap(":icon_class"),
                                            i18n("Symbol Browser")
                                           );
    mPlugin = plugin;
    //plugin->mViews.append(this);
    mSymbolsView = new SymbolBrowserView(mMainView);
    //connect(mSymbolsView, SIGNAL(skipToLine(int)), this, skipToLine

    connect(mainWindow, SIGNAL(viewCreated(KTextEditor::View*)), this, SLOT(createParser(KTextEditor::View*)));
    connect(mainWindow, SIGNAL(viewChanged()), this, SLOT(activateParser()));

    globalSettingsChanged();
}


/**********************************************************
 *
 **********************************************************/
KateSymbolBrowserView::~KateSymbolBrowserView()
{
    mPlugin->mViews.removeAll(this);
}


/**********************************************************
 *
 **********************************************************/
void KateSymbolBrowserView::globalSettingsChanged()
{
    KConfigGroup config(KGlobal::config(), CONFIG_GROUP);

    mSymbolsView->setSettingsActivateOnSingleClick(config.readEntry("ActivateOnSingleClick", true));

    mSymbolsView->setSettingsDetail(config.readEntry("Detail", true));
    mSymbolsView->setSettingsSort(config.readEntry("Sort", true));
    mSymbolsView->setSettingsExpand(config.readEntry("Expand", true));
}


/**********************************************************
 *
 **********************************************************/
void KateSymbolBrowserView::createParser(KTextEditor::View *view)
{
    KTextEditor::Document *doc = view->document();
    KateDocumentParser *parser = new KateDocumentParser(mSymbolsView, doc);

    connect(doc, SIGNAL(textChanged(KTextEditor::Document*)), parser,  SLOT(refresh()));
    connect(parser, SIGNAL(destroyed(QObject*)), this,  SLOT(parserDestroyed(QObject*)));

    parser->refresh();
    mParsers.append(parser);
}


/**********************************************************
 *
 **********************************************************/
void KateSymbolBrowserView::activateParser()
{
    if (!mainWindow()->activeView())
        return;

    KTextEditor::Document *doc = mainWindow()->activeView()->document();

    QList<KateDocumentParser*>::iterator i;
    for (i = mParsers.begin(); i != mParsers.end(); ++i)
    {
        if ((*i)->kateDocument() == doc)
            (*i)->activate();
    }
}


/**********************************************************
 *
 **********************************************************/
void KateSymbolBrowserView::parserDestroyed(QObject * obj)
{
    mParsers.removeAll(qobject_cast<KateDocumentParser*>(obj));
}
